package me.maiz.babyisland.config;

import me.maiz.babyisland.component.ShiroMybatisRealm;
import me.maiz.babyisland.component.ShiroMybatisRealm;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        //确保静态资源和登录页面可以被访问
        chainDefinition.addPathDefinition("/layui/**", "anon");
        chainDefinition.addPathDefinition("/static/**", "anon");
        chainDefinition.addPathDefinition("/login", "anon");
        chainDefinition.addPathDefinition("/", "anon");
        //其他页面需要认证后访问
        chainDefinition.addPathDefinition("/**", "authc");

        return chainDefinition;
    }


    @Bean
    public Realm realm() {
        // uses 'classpath:shiro-users.properties' by default
        ShiroMybatisRealm realm = new ShiroMybatisRealm();
        // Caching isn't needed in this example, but we can still turn it on
        realm.setCachingEnabled(true);
        return realm;
    }


    @Bean("shiroCacheManager")
    public CacheManager cacheManager() {
        // Caching isn't needed in this example, but we will use the MemoryConstrainedCacheManager for this example.
        return new MemoryConstrainedCacheManager();
    }
}
